package rollercoaster.environment.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import rollercoaster.environment.interfaces.IModelReader;
import rollercoaster.commons.Model;
import rollercoaster.commons.Vertex2D;
import rollercoaster.commons.Vertex3D;

public class ModelMaker {
	private ArrayList<Index> iList;
	private List<Integer> indices;
	private float[] indexedVertices;
	private float[] indexedNormals;
	private float[] indexedTextures;
	private IModelReader modelReader;
	private Model model;
	
	
	
	
	public ModelMaker(IModelReader modelReader) {
		this.modelReader=modelReader;
	}
	
	public Model makeModel(){
		//System.out.println("size facce: "+modelReader.getFaces().size()+"\n");
		//System.out.println("size vertici: "+modelReader.getVertices().size()+"\n");
		//System.out.println("size texture: "+modelReader.getTexCoord().size()+"\n");
		makeIndeces(modelReader.getFaces());
		indexNormals(modelReader.getNormals());
		indexVertices(modelReader.getVertices());
		indexTextures(modelReader.getTexCoord());
		model = new Model(this.indexedVertices,this.indexedNormals,convertIndices(),this.indexedTextures);
		return model;
	}
	

	public void setModelReader(IModelReader modelReader) {
		this.modelReader = modelReader;
	}

	public void makeIndeces(ArrayList<ObjFace> faces){
		
		Index index=new Index();
		
		int ii;
		
		iList=new ArrayList<Index>();
		indices=new ArrayList<Integer>();
		
		for (Iterator iterator = faces.iterator(); iterator.hasNext();) {
			ObjFace face=(ObjFace) iterator.next();
			for (int i = 0; i < face.getVerteces().length; i++) {
				
				index.setFaceVertex(face.getVerteces()[i]);
				index.setFaceNormal(face.getNormals()[i]);
				index.setFaceTexture(face.getTexCoord()[i]);
				
				ii = iList.indexOf(index);
				if (ii == -1) {
					Index newIndex=new Index();
					newIndex.setFaceVertex(face.getVerteces()[i]);
					newIndex.setFaceNormal(face.getNormals()[i]);
					newIndex.setFaceTexture(face.getTexCoord()[i]);
					iList.add(newIndex);
					indices.add(iList.size()-1);
				}
				else
					indices.add(ii);
			}	
		}
	}	
	
	public void indexVertices(ArrayList<Vertex3D> vertices){
		
		indexedVertices=new float[indices.size()*3];
		int vertexIndex;

		for (int i = 0; i < iList.size(); i++) {
			
			vertexIndex=(iList.get(i).getFaceVertex())-1;
			//System.out.println("vertex index: "+vertexIndex);
			indexedVertices[3*i]=vertices.get(vertexIndex).getX();
			indexedVertices[3*i+1]=vertices.get(vertexIndex).getY();
			indexedVertices[3*i+2]=vertices.get(vertexIndex).getZ();
		}
	}
	
	public void indexTextures(ArrayList<Vertex2D> textures){
		
		indexedTextures=new float[indices.size()*2];
		int textureIndex;
		for (int i = 0; i < iList.size(); i++) {
			
			textureIndex=(iList.get(i).getFaceTexture())-1;
			try {
				indexedTextures[2*i]=textures.get(textureIndex).getX();
				indexedTextures[2*i+1]=textures.get(textureIndex).getY();
			} catch (Exception e) {
				System.out.println("Cordinate texture non trovate!");
				indexedTextures[2*i]=0;
				indexedTextures[2*i+1]=0;
			
			}
			
	
		}
	}
	
	public void indexNormals(ArrayList<Vertex3D> normals){
		indexedNormals=new float[indices.size()*3];
		int normalIndex;

		for (int i = 0; i < iList.size(); i++) {
			normalIndex=(iList.get(i).getFaceNormal())-1;
			
				indexedNormals[3*i]=normals.get(normalIndex).getX();
				indexedNormals[3*i+1]=normals.get(normalIndex).getY();
				indexedNormals[3*i+2]=normals.get(normalIndex).getZ();
			
				
			
			
		}
	}
	

	public short[] convertIndices(){
		short[] shortIndices= new short[indices.size()];
		for (int i = 0; i < indices.size(); i++) {
			shortIndices[i]=indices.get(i).shortValue();
		}
		return shortIndices;
	}
}
